package scr;

public abstract class Test {
    public static void main(String[] args) {
        Student[] array = new Student[3];
        Student s1 = new Student(1, "张三", 18);
        Student s2 = new Student(2, "李四", 19);
        Student s3 = new Student(3, "王五", 20);
        array[0] = s1;
        array[1] = s2;
        array[2] = s3;
        System.out.println("添加学生信息");
        Student s4 = new Student(4, "赵六", 21);
        boolean flag = idJudge(array, s4.getId());
        if (flag) {
            System.out.println("学号重复");
        } else {
            int index = arrJudge(array);
            if (index == array.length) {
                Student[] newArr = creatArr(array);
                newArr[index] = s4;
                printArr(newArr);
            } else {
                array[index] = s4;
                printArr(array);
            }
        }
        System.out.println("删除学生信息");
 //通过id删除学生对象
        int delId = 3;
        int delIndex = getIndex(array, delId);
        if (delIndex > -1) {
            array[delIndex] = null;
        } else {
            System.out.println("没有这个学号删除失败！");
        }
        printArr(array);
    }
    private static void printArr(Student[] array) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] != null) {
                System.out.printf("id: %s, name: %s, age: %s", array[i].getId(), array[i].getName(), array[i].getAge());
                System.out.println();
            }
        }
    }

    private static Student[] creatArr(Student[] array) {
        Student[] newArr = new Student[array.length + 1];
        for (int i = 0; i < array.length; i++) {
            newArr[i] = array[i];
        }
        return newArr;
    }

    private static int arrJudge(Student[] array) {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] != null) {
                count++;
            }
        }
        return count;
    }

    private static boolean idJudge(Student[] array, int id) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] != null) {
                if (array[i].getId() == id) {
                    return true;
                }
            }
        }
        return false;

    }

    public static int getIndex(Student[] array, int id) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] != null) {
                if (array[i].getId() == id) {
                    return i;
                }
            }
        }
        return -1;
    }
}












